package doa;

/**
 * @author User
 */
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import models.Product;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import service.HibernateUtil;

public class ProductDAO {

    public ProductDAO() {
    }

    public static List<Product> getAllProducts() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Product> product_list = session.createCriteria(Product.class).list();

        return product_list;
    }

    public static List<Product> getAllProductsFromCategory(int category_id) {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Query queryResult = session.createQuery("FROM Product WHERE category_id = '" + category_id + "'");
        List<Product> product_list = queryResult.list();
        tx.commit();
        return product_list;
    }

    public static Product getProduct(int product_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Product product = (Product) session.get(Product.class, product_id);
        tx.commit();

        return product;
    }

    public static ArrayList insertProduct(Product product) {
        ArrayList validation_errors = new ArrayList();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        // product = getProductFromRequest(request);
        session.save(product);
        tx.commit();
        return validation_errors;
    }

    public static ArrayList editProduct(Product product) {
        ArrayList validation_errors = new ArrayList();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        session.update(product);
        tx.commit();
        return validation_errors;
    }

    public static void deleteProduct(int product_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Product product = (Product) session.load(Product.class, product_id);
        session.delete(product);
        tx.commit();
    }

    private static Product getProductFromRequest(HttpServletRequest request) {
        Product p = new Product();
        if (request.getParameter("product_id") != null && !request.getParameter("product_id").isEmpty()) {
            p.setProduct_id(Integer.parseInt(request.getParameter("product_id")));
        }
        if (request.getParameter("product_name") != null && !request.getParameter("product_name").isEmpty()) {
            p.setProduct_name(request.getParameter("product_name"));
        }
        if (request.getParameter("product_image") != null && !request.getParameter("product_image").isEmpty()) {
            p.setProduct_image(request.getParameter("product_image"));
        }
        return p;
    }
}
